#!/usr/bin/perl
##
# @param $1 install home
# @param $2 delimiter
# @param $3 host
# @param $4 port
# @param $5 username
# @param $6 password
#
use strict;
use Time::Local;

my ($MYSQL_HOME,$DELIM,$HOST,$PORT,$USER,$PASS) = @ARGV;
my $PARAM="";
$PARAM="$PARAM -h\"$HOST\"" if (defined($HOST) && $HOST ne "");
$PARAM="$PARAM -P\"$PORT\"" if (defined($PORT) && $PORT ne "");
$PARAM="$PARAM -u\"$USER\"" if (defined($USER) && $USER ne "");
$PARAM="$PARAM -p\"$PASS\"" if (defined($PASS) && $PASS ne "");
my $MYSQL="$MYSQL_HOME/bin/mysql";
my $MYSQLADMIN="$MYSQL_HOME/bin/mysqladmin";
if ($^O =~ m/MSWin32/) {
  $MYSQL =~ s/\//\\/g;
  $MYSQLADMIN =~ s/\//\\/g;
}
# Check if mysqld is alive
my $retmsg=`$MYSQLADMIN $PARAM ping 2>&1`;
chomp($retmsg);
if ( "$retmsg" ne "mysqld is alive" ) {
    print "em_result=Status${DELIM}Down\n";
    exit 0;
}
#print "em_result=Status${DELIM}Up\n";

#get error log file name
my $SQL="show variables like 'log_error'";
my $logfile=`$MYSQL -s --skip-column-names $PARAM -e"$SQL"`;
chomp($logfile);
my $key;
($key , $logfile) = split /[ \t]/,$logfile;

my $datadir=`$MYSQL -s --skip-column-names $PARAM -e "show variables like 'datadir'"`;
chomp($datadir);
($key , $datadir) = split /[ \t]/, $datadir;
chomp($datadir);

if ($logfile)
{
  
  if (substr($logfile,0,2) eq './')
  {
    $logfile = $datadir.$logfile;
  }
}
else
{
  my $hostname=`hostname`;
  chomp($hostname);
  $logfile = $datadir.$hostname.".err";
}


#print "logfile is $logfile\n";

my $lastposfile="/tmp/errorlog_allerr_last_pos_".$HOST.":".$PORT;

my $lastpos;
my $lastcontent;
my $is_old=0;
if ( open ( my $lpfh, "<", $lastposfile ) )
{
  $lastpos = readline($lpfh);
  chomp ($lastpos);
  $lastcontent = readline($lpfh);
  chomp ($lastcontent);

  close ($lpfh);


  open(my $fh, "<" , $logfile) or die "$0: can't open $logfile: $!";
  while ( my $line = <$fh> )
  {
    if ( $. == $lastpos )
    {
      chomp ($line);
      if ( $line eq $lastcontent )
      {
        $is_old=1;
      }
      else
      {
        $is_old=0;
      } 
      last;
    }
  }

  close ($fh);

}

my $curtime = time();
my $lastlinenum;
my $lastlinecontent;
open(my $fh, "<" , $logfile) or die "$0: can't open $logfile: $!";
while ( my $line = <$fh> )
{
  next if ( $is_old and $. <= $lastpos ); # continue to next loop until get line after lastpos.

  chomp ($line);

  $lastlinenum = $.;
  $lastlinecontent = $line;

  next if (! $line );

  #print "$line\n";
  #only analyze lines beginning with timestamp
  if ( $line =~ /^([0-9]{2})([0-9]{2})([0-9]{2}) ([ 0-9][0-9]):([0-9][0-9]):([0-9][0-9]) *(.*$)/ )
  {
    my $year = $1;
    my $mon = $2;
    my $day = $3;
    my $hour = $4;
    my $min = $5;
    my $sec = $6;
    my $message = $7;
    $year = "20".$year;
    $hour =~ s/^ /0/;

    ## Do not analyze logs two hours ago if the logfile is a new file.
    if (! $is_old)
    {
      my $logtime = timelocal($sec, $min, $hour, $day, $mon - 1, $year); 
      next if ( $logtime < $curtime - 7200 );
    }

    print "em_result=$year-$mon-$day $hour:$min:$sec|$lastlinenum|$message\n";
  }


}

close($fh);

if ( $lastlinenum )
{
  open ( my $lpf, ">", $lastposfile ) or die "$0: can't open $lastposfile: $!";
  print $lpf "$lastlinenum\n";
  print $lpf "$lastlinecontent\n";
  close ( $lpf );
}

exit 0
